//
//  MSPopupEntity.m
//  MSPopupsManager
//
//  Created by zz on 22.4.24.
//

#import "MSPopupEntity.h"
#import "MSPopupManager.h"

@interface MSPopupEntity()<UIGestureRecognizerDelegate>

/// 定时器
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSTimeInterval dismissTime;

@end

@implementation MSPopupEntity

- (void)setConfig:(MSPopupConfigure *)config {
    _config = config;
    if (config.dismissDuration > 0) {
        _dismissTime = config.dismissDuration;
    }
}


//给自定义的弹窗内容设置圆角
- (void)setupCustomViewCorners {
    [self.popupBgView layoutIfNeeded];
    BOOL isSetCorner = NO;
    if (self.config.rectCorners & UIRectCornerTopLeft) {
        isSetCorner = YES;
    }
    if (self.config.rectCorners & UIRectCornerTopRight) {
        isSetCorner = YES;
    }
    if (self.config.rectCorners & UIRectCornerBottomLeft) {
        isSetCorner = YES;
    }
    if (self.config.rectCorners & UIRectCornerBottomRight) {
        isSetCorner = YES;
    }
    if (self.config.rectCorners & UIRectCornerAllCorners) {
        isSetCorner = YES;
    }
    
    if (isSetCorner && self.config.rectCorners > 0) {
        UIView *cornerRadiusView = [self contentView];
//        if ([self.popupObj respondsToSelector:@selector(needSetCornerRadiusView)]) {
//            UIView *view = [self.popupObj needSetCornerRadiusView];
//            if (view && CGSizeEqualToSize(view.bounds.size, CGSizeZero) != YES) {
//                cornerRadiusView = view;
//            }
//        }
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:cornerRadiusView.bounds
                                                    byRoundingCorners:self.config.rectCorners
                                                          cornerRadii:CGSizeMake(self.config.cornerRadius, self.config.cornerRadius)];
        CAShapeLayer * layer = [[CAShapeLayer alloc]init];
        layer.frame = cornerRadiusView.bounds;
        layer.path = path.CGPath;
        cornerRadiusView.layer.mask = layer;
    }
}

//开始倒计时
- (void)startCountTime {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerLoopExecute) userInfo:nil repeats:YES];
    //加入runloop循环池
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    //开启定时器
    [_timer fire];
}


- (void)closeTimer {
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _dismissTime = self.config.dismissDuration;//定时器计数复原
}

- (void)timerLoopExecute {
    if (self.dismissTime < 1) {
        [self closeTimer];
        if  (!self.config.isOnlyCountModel) {
            //关闭弹窗
            [MSPopupManager dismissWithPopup:self.popupObj];
        }
        return;
    }
    self.dismissTime --;
    if ([self.popupObj respondsToSelector:@selector(countTimeWithCount:identifier:)]) {
        [self.popupObj countTimeWithCount:self.dismissTime identifier:self.config.identifier];
    }
}


- (UIView *)contentView {
    return (UIView *)[_popupObj supportAtmospherePopupView];
}

//添加相关手势
- (void)addGestureRecognizer {
    //弹窗背景添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popupBgViewTap:)];
    tap.delegate = self;
    [self.popupBgView addGestureRecognizer:tap];
    
//    //添加上滑手势
//    if (self.config.needNoticeBarPanGesture) {
//        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(popupBgViewPan:)];
//        panGesture.delegate = self;
//        [self.popupBgView addGestureRecognizer:panGesture];
//    }

}

//背景手势回调
- (void)popupBgViewTap:(UIGestureRecognizer *)gestureRecognizer {
    if (self.config.clickOutsideDismiss) {
        [self.popupBgView endEditing:YES];
        [MSPopupManager dismissWithPopup:self.popupObj];
    }
}

@end
